package cn.water.rocketmq.controller;

import cn.water.rocketmq.param.User;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @GetMapping("/user")
    public String send(User user){
        //封装消息
        Message<User> message = MessageBuilder.withPayload(user).build();
        rocketMQTemplate.asyncSend("user-topic1", message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("消息发送成功");
            }

            @Override
            public void onException(Throwable throwable) {
                System.out.println("消息发送失败");
            }
        },500,2);
        System.out.println("消息已发送");
        return "send success!";
    }
}
